#!/usr/bin/perl -w

#=======================================================================
# extract_mail
# File ID: 953fa428-5d39-11df-8ca0-90e6ba3022ac
# Extracts messages from a mailbox into separate files
# ©opyleft 2002 Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later.
#=======================================================================

use strict;
use Time::Local;

my @Msg = ();
my $Date = "";
my $date_done = 0;
my $dest_dir = "out";
my $TZ_ext = "Z";
my %mon_str = (
	"jan" => "01",
	"feb" => "02",
	"mar" => "03",
	"apr" => "04",
	"may" => "05",
	"jun" => "06",
	"jul" => "07",
	"aug" => "08",
	"sep" => "09",
	"oct" => "10",
	"nov" => "11",
	"dec" => "12"
);

-d $dest_dir || mkdir($dest_dir, 0777) || die("mkdir($dest_dir): $!");

while (<>) {
	if (/^From /) {
		D("========= $_");
		if (scalar(@Msg)) {
			if (length($Date)) {
				($Date =~ /^\d{8}T\d{6}$/) || die("Line $.: Invalid date format: \"$Date\"");
				my $file_name = "$dest_dir/mail.${Date}$TZ_ext.txt";
				-e $file_name && (warn("$file_name: File already exists"), $Date .= "b");
				open(ToFP, ">$file_name") || die("$file_name: Opening for write: $!");
				for (@Msg) {
					print(ToFP $_);
				}
				close(ToFP);
			} else {
				warn("Datoen var tom");
			}
		}
		($Date, $date_done, $TZ_ext) = ("", 0, "");
		@Msg = ();
	}
	push(@Msg, $_);
	if (/^Date: / && !$date_done) {
		if (
				/^\D* # Before the date
				(\d+) # Day
				\s+
				(\S+) # Month
				\s+
				(\d+) # Year
				\s+
				(\d+)   # Hours
				:
				(\d+)   # Minutes
				:
				(\d+)   # Seconds
				\s*
				(\S*)   # Time zone
				/x
			) {
			my ($Year, $Mon, $Day, $Hour, $Min, $Sec, $Timezone) = ($3, $2, $1, $4, $5, $6, "");
			(defined($7) && ($Timezone = $7)) || warn("Line $.: Date missing time zone");
			$Mon = lc($Mon);
			D("Day = \"$Day\", Mon = \"$Mon\"\n");
			defined($mon_str{$Mon}) || (die("Unknown month: \"$Mon\""), ($Mon = "XX"));
			$Mon = $mon_str{$Mon};
			my $curr_date = timelocal($Sec, $Min, $Hour, $Day, $Mon-1, $Year);
			my $zone_diff = 0;
			if ($Timezone =~ /^(\+|-)(\d\d)(\d\d)$/) {
				$zone_diff = $2*3600 + $3*60;
				($1 eq "+") && ($zone_diff = 0-$zone_diff);
			} elsif ($Timezone =~ /^GMT$/i) {
				$zone_diff = 0;
			} else {
				warn("Line $.: Unknown timezone format");
				$TZ_ext = "";
			}
			D("curr_date before tz: \"$curr_date\", Timezone = \"$Timezone\"\n");
			$zone_diff = 0; # Temporary until the timezone problems are eliminated.
			$curr_date += $zone_diff;
			D("curr_date after tz : \"$curr_date\", Timezone = \"$Timezone\"\n");
			D("zone_diff = \"$zone_diff\"\n");
			($Sec, $Min, $Hour, $Day, $Mon, $Year) = gmtime($curr_date);
			$Year += 1900; # Urgh
			$Mon   = sprintf("%02u", $Mon+1); # Urgh II
			$Day   = sprintf("%02u", $Day);
			$Hour  = sprintf("%02u", $Hour);
			$Min   = sprintf("%02u", $Min);
			$Sec   = sprintf("%02u", $Sec);
			$Date = "$Year$Mon${Day}T$Hour$Min$Sec";
			$date_done = 1;
		} else {
			warn("Line $.: Weird date: \"$Date\"");
		}
	}
}

sub D {
	print(STDERR @_);
}

=pod

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=cut

#### End of file extract_mail ####
